angular.module("application")
.controller('categoryCtrl',['$scope', '$location', '$routeParams', 'categoryProcess', 'BASE_URL', 
	function($scope, $location, $routeParams, categoryProcess, BASE_URL) 
{	
	$scope.$parent.loadingView = true;

	$scope.catsArr = [];
	$scope.catType = $routeParams.type;
	if($scope.catType == 'vn') $scope.strtype = "VIỆT NAM";
	else if($scope.catType == 'wo') $scope.strtype = "THẾ GIỚI";
	else $location.path('/category/vn');

	$scope.detailcat = false;
	$scope.detailCatView = {};
	$scope.lessonArr = [];

	$scope.$on('$viewContentLoaded', function() { // Load content effexts after content is loaded
		fullPageEffect();
	});

	categoryProcess.getData($scope.catType).then(function(data) {
		setTimeout(function() {
			$scope.$apply(function() {
				$scope.$parent.loadingView = false;
			})
		},1000);
		console.log(data);
		$scope.catsArr = data;
	});

	$scope.listDetail = function(id, num) {
		if(num <= 0) return;
		categoryProcess.getLessonByCat(id).then(function(data) {

			angular.forEach($scope.catsArr, function(parent) {
				if(parent._id == id) {
					$scope.detailCatView.catName = parent.name;
					return;
				}
				for(i in parent.childs) {
					if(parent.childs[i]._id == id) {
						$scope.detailCatView.catName = parent.childs[i].name;
						return;
					}
				}
			});
			$scope.lessonArr = data;
			$scope.detailcat = true;
			setTimeout(function() {
				categoryProcess.resizeList();	
			},100);
		});
	}
	
}])

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------

.controller('catCtrl', function($scope, BASE_URL, $http, $routeParams, categoryProcess) {
	catId = $routeParams.catid;
	$scope.lessonArr = [];

	$scope.$on('$viewContentLoaded', function() { // Load content effexts after content is loaded
		fullPageEffect();
	});

	categoryProcess.getCatInfo(catId).then(function(data) {
		$scope.category = {
			"catName": data.name,
			"type": data.type
		}
		if(data.type == "vn") {
			$scope.category.strType = "LS Việt Nam";
		}
		else if(data.type == "wo") {
			$scope.category.strType = "LS Thế giới";
		}
	});

	categoryProcess.getLessonByCat(catId).then(function(data) {
		$scope.lessonArr = data;
		setTimeout(function() {
			$scope.$apply(function() {
				$scope.$parent.loadingView = false;
			})
		},500);
		setTimeout(function() {
			categoryProcess.resizeList();	
		},550);
		console.log(data);
	});

})

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//Services
.factory('categoryProcess', function(BASE_URL, $http) {
	return {
		getData: function(type) {
			var catsArr = [];
			return $http.post(BASE_URL + 'index.php/application_api/category/getCats/' + type)
			.then(function(promise) {
				listId = [];
				data = promise.data;
				console.log(data);

				angular.forEach(data, function(dt) {
					listId.push(dt._id);
					if(dt.parent == null) {
						dt.childs = [];
						catsArr.push(dt);
					}
				});

				angular.forEach(catsArr, function(parent) {
					angular.forEach(data, function(dt) {
						if(dt.parent == parent._id) {
							parent.numLesson += dt.numLesson;
							parent.childs.push(dt);
						}
					})
				})
				return catsArr;
			});
		},
		getLessonByCat: function(id) {
			return $http.post(BASE_URL + 'index.php/application_api/category/getLsByCat/' + id)
			.then(function(promise) {
				return promise.data;
			});
		},
		getCatInfo: function(id) {
			return $http.post(BASE_URL + 'application_api/category/getHeaderCat/' + id)
			.then(function(promise) {
				return promise.data;
			})
		},
		resizeList: function() {
			$('.lesson-list li').each(function() {
				w = $(this).innerWidth();
				h = w * 4 / 6;
				$(this).css('height', h);
			});

			$(window).resize(function() {
				$('.lesson-list li').each(function() {
					w = $(this).innerWidth();
					h = w * 4 / 6;
					$(this).css('height', h);
				});
			})
		}
	}
})


// ==============================================================================================================
// ==============================================================================================================
// ==============================================================================================================
// ==============================================================================================================

.controller('eventCtrl', function($scope, BASE_URL, $routeParams, $location, eventProcess) {
	$scope.eventName = "";
	$scope.lessonList = [];
	$scope.questionList = [];

	$scope.$on('$viewContentLoaded', function() { // Load content effexts after content is loaded
		fullPageEffect();
	});

	var evId = $routeParams.id;
	eventProcess.getEventName(evId).then(function(data) {
		$scope.eventName = data;
		return eventProcess.getLessonByEvent(evId);
	}).then(function(data) {
		angular.forEach(data, function(ls) {
			$scope.lessonList.push(ls);
		});
		return eventProcess.getQuestionByEvent(evId);
	}).then(function(data) {
		console.log(data);
		angular.forEach(data, function(qs) {
			$scope.questionList.push(qs);
		});
		setTimeout(function() {
			$scope.$apply(function() {
				$scope.$parent.loadingView = false;
			})
		},400);
		setTimeout(function() {
			eventProcess.resizeList();	
		},500);
	});
})

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------

.factory('eventProcess', function($http, BASE_URL) {
	return {
		getEventName: function(id) {
			return $http.post(BASE_URL + 'index.php/application_api/timeline/getEventName/' + id)
			.then(function(prm) {
				return prm.data.eventName;
			})
		},
		getLessonByEvent: function(id) {
			return $http.post(BASE_URL + 'index.php/application_api/lesson/getLsByEvent/' + id)
			.then(function(prm) {
				arrx = [];
				angular.forEach(prm.data, function(ls) {
					str = ls.title.toLowerCase();
					if((str.search('ôn tập') == -1 && str.search('tổng kết') == -1) || str.search('bài') != 0) {
						arrx.push(ls);
					}
				})
				return arrx;
			})
		},
		getQuestionByEvent: function(idEv) {
			return $http.post(BASE_URL + 'index.php/application_api/question/getQsByEvent/' + idEv)
			.then(function(prm) {
				return prm.data;
			})
		},
		resizeList: function() {
			$('.lesson-list li').each(function() {
				w = $(this).innerWidth();
				h = w * 4 / 6;
				$(this).css('height', h);
			});

			$(window).resize(function() {
				$('.lesson-list li').each(function() {
					w = $(this).innerWidth();
					h = w * 4 / 6;
					$(this).css('height', h);
				});
			})
		}
	}
})


// ==============================================================================================================
// ==============================================================================================================
// ==============================================================================================================
// ==============================================================================================================

.controller('classCtrl', ['$scope', '$routeParams', 'classProcess', 'BASE_URL', 
	function($scope, $routeParams, classProcess, BASE_URL) 
{	
	classId = $routeParams.classId;
	classProcess.getMenu(classId).then(function(menu) {
		$scope.Menu = menu.sections;
		console.log(menu.sections);
		$scope.Class = menu.class;

		setTimeout(function() {
			$scope.$apply(function() {
				$scope.$parent.loadingView = false;
			})
		},1000);
	})
	

}])

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------

.factory('classProcess', function($http, BASE_URL) {
	return {
		getClass: function() {
			return $http.post(BASE_URL + 'application_api/classes/getClasses')
			.then(function(promise) {
				console.log(promise.data);
				return promise.data;
			})
		},
		getMenu: function(classId) {
			var menu = {};
			return $http.post(BASE_URL + 'application_api/classes/getMenu/' + classId)
			.then(function(promise) {
				menu = promise.data;
				return $http.post(BASE_URL + 'application_api/category/getAllCats');

			}).then(function(promise){
				cat = promise.data;
				angular.forEach(menu.sections, function(sect) {
					// get section name
					for(c in cat) {
						if(cat[c]._id == sect.section) {
							sect.sectionName = cat[c].name;
							break;
						};
					};
					for(ch in sect.chapters) {
						// get chapter name
						for(c in cat) {
							if(cat[c]._id == sect.chapters[ch].chapter) {
								sect.chapters[ch].chapterName = cat[c].name;
								break;
							};
						};
					}

				});
				return $http.post(BASE_URL + 'application_api/lesson/getLessonList');

			}).then(function(promise) {
				ls = promise.data;
				angular.forEach(menu.sections, function (sect) {
					angular.forEach(sect.chapters, function (chap) {
						angular.forEach(chap.lessons, function(lesson) {
							for(i in ls) {
								if(ls[i]._id == lesson.lesson) {
									lesson.lessonName = ls[i].title;
								}
							}
						});
					});
				});

				var order = 1;
				angular.forEach(menu.sections, function (sect) {
					angular.forEach(sect.chapters, function (chap) {
						angular.forEach(chap.lessons, function(lesson) {
							lesson.lessonName = 'Bài ' + order + ': ' + lesson.lessonName;
							order++;
						})
					})
				})

				return menu;
			})
		}
	}
});